package scsc.pm;

import java.util.*;

import scsc.pub.data.DeptMajorLead;
import scsc.pub.db.*;
import scsc.pub.tools.*;
import java.util.ArrayList;
import scsc.web.system.data.DeptMajorLeadData;
import scsc.pub.exception.*;

public class DeptMajorLeadPm {
  private static Logger logger = Logger.getLogger(DeptMajorLeadPm.class);
  public DeptMajorLeadPm() {
  }


  public void createDeptMajorLead(DBManage dbmanage,
		  DeptMajorLeadData formdata) throws
      UnifyUserException {
	  DeptMajorLead data = new DeptMajorLead();    
	  data.setDeptCode( formdata.getDeptCode() );
	  data.setMajorLead( formdata.getMajorLead() );
	  data.setLeadType( formdata.getLeadType() );
	  data.setRemark( formdata.getRemark() );
	  dbmanage.sqlSave(data);
	  formdata.setId(data.getId());
  }


  public void deleteDeptMajorLead(DBManage dbmanage, String id) throws
      UnifyUserException {
    String hql = "delete from scsc.pub.data.DeptMajorLead where id=" + id;
    dbmanage.sqlDelete(hql);
  }


  public void modifyDeptMajorLead(DBManage dbmanage,
		  DeptMajorLeadData formdata) throws
      UnifyUserException {
	  DeptMajorLead data = new DeptMajorLead();    
	  data.setId( formdata.getId() );
	  data.setDeptCode( formdata.getDeptCode() );
	  data.setMajorLead( formdata.getMajorLead() );
	  data.setLeadType( formdata.getLeadType() );
	  data.setRemark( formdata.getRemark() );
	  dbmanage.sqlUpdate(data);

  }


  public void selectDeptMajorLead(DBManage dbmanage, Integer id,
                                  String deptCode, String majorLead,String leadType,
                                  PageListData footer) {
    String sqlStr = "select new scsc.web.system.data.DeptMajorLeadData" +
    		"(dm.id,dm.deptCode,dept.deptname,dm.majorLead,oper.operatorname,dm.leadType,dm.remark)" +
    		" from scsc.pub.data.DeptMajorLead as dm,scsc.pub.data.Operator as oper,scsc.pub.data.Department as dept"+
    		" where dm.deptCode=dept.deptcode and dm.majorLead=oper.operatorid";
    if (id != null && id.intValue() >0) {
      sqlStr = sqlStr + " and dm.id =" + id;
    }
    if (deptCode!=null && deptCode.length() > 0) {
      sqlStr = sqlStr + " and dm.deptCode like '%" + deptCode + "%'";
    }
    if (majorLead!=null && majorLead.length() > 0) {
        sqlStr = sqlStr + " and dm.majorLead like '%" + majorLead + "%'";
      }
    if (leadType!=null && leadType.length() > 0) {
        sqlStr = sqlStr + " and dm.leadType like '%" + leadType + "%'";
      }


    dbmanage.sqlQueryPage(sqlStr, footer);
  }

  public ArrayList selectDeptMajorLead(DBManage dbmanage, Integer id,
          String deptCode, String majorLead,String leadType) {
	  String sqlStr = "select new scsc.web.system.data.DeptMajorLeadData" +
		"(dm.id,dm.deptCode,dept.deptname,dm.majorLead,oper.operatorname,dm.leadType,dm.remark)" +
		" from scsc.pub.data.DeptMajorLead as dm,scsc.pub.data.Operator as oper,scsc.pub.data.Department as dept"+
		" where dm.deptCode=dept.deptcode and dm.majorLead=oper.operatorid";
		if (id != null && id.intValue() >0) {
		sqlStr = sqlStr + " and dm.id =" + id;
		}
		if (deptCode!=null && deptCode.length() > 0) {
		sqlStr = sqlStr + " and dm.deptCode like '%" + deptCode + "%'";
		}
		if (majorLead!=null && majorLead.length() > 0) {
		  sqlStr = sqlStr + " and dm.majorLead like '%" + majorLead + "%'";
		}
		if (leadType!=null && leadType.length() > 0) {
		  sqlStr = sqlStr + " and dm.leadType like '%" + leadType + "%'";
		}

	    List result = dbmanage.sqlQuery(sqlStr);
	    return (ArrayList) result;
  }
   
  public ArrayList selectRestLead(DBManage dbmanage){
	  String sqlStr="select new scsc.web.system.data.SysOperatorData(operator.operatorid,operator.operatorname,operator.deptcode,operator.deptcode,operator.password,operator.regdate,operator.lastpassword,operator.lifecycle,operator.stat)  from scsc.pub.data.Operator as operator,scsc.pub.data.Operatorgroup as og";
	  //sqlStr=sqlStr + " where operator.operatorid=og.comp_id.operatorid and og.comp_id.groupcode in ('10000001','10000002') and operator.operatorid not in (select distinct dm.majorLead from scsc.pub.data.DeptMajorLead as dm)";
	  sqlStr=sqlStr + " where operator.operatorid=og.comp_id.operatorid and og.comp_id.groupcode in ('10000001','10000002')";
	  List result = dbmanage.sqlQuery(sqlStr);
	  return (ArrayList) result;
  }
  public String IsMajorLeadExisted(DBManage dbmanage,String deptCode){
	  String sqlStr="select new scsc.web.system.data.DeptMajorLeadData(dm.id,dm.deptCode,dm.majorLead,dm.leadType) from scsc.pub.data.DeptMajorLead as dm where dm.deptCode='" + deptCode +"'";
	  List result = dbmanage.sqlQuery(sqlStr);
	  if (result!=null && result.size()>0)
		  return "1";
	  else
		  return "0";
  }
  public ArrayList selectDeptMajorLead(DBManage dbmanage, Integer id,
          String deptCode, String majorLead){
	  String sqlStr="select new scsc.web.system.data.DeptMajorLeadData(dm.id,dm.deptCode,dm.majorLead,dm.leadType) from scsc.pub.data.DeptMajorLead as dm where 1=1";
	  if (id!=null && id.intValue()!=0)
		  sqlStr=sqlStr + " and dm.id=" + id;
	  if (deptCode!=null && deptCode.length()>0)
		  sqlStr=sqlStr + " and dm.deptCode like '%" + deptCode + "%'";
	  if (majorLead!=null && majorLead.length()>0)
		  sqlStr=sqlStr + " and dm.majorLead like '%" + majorLead + "%'";
	  List result = dbmanage.sqlQuery(sqlStr);
	  return (ArrayList) result;
  }
  

}
